package com.yic.module.mes.service.processrouteinfo;

import cn.hutool.core.util.ObjectUtil;
import com.yic.module.mes.dal.dataobject.processRoute.ProcessRouteDO;
import com.yic.module.system.enums.dict.SystemBooleanEnum;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import java.util.stream.Collectors;

import com.yic.module.mes.controller.admin.processrouteinfo.vo.*;
import com.yic.module.mes.dal.dataobject.processrouteinfo.ProcessRouteInfoDO;
import com.yic.framework.common.pojo.PageResult;

import com.yic.module.mes.convert.processrouteinfo.ProcessRouteInfoConvert;
import com.yic.module.mes.dal.mysql.processrouteinfo.ProcessRouteInfoMapper;

import static com.yic.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yic.module.mes.enums.ErrorCodeConstants.*;

/**
 * 工艺路线详细配置 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class ProcessRouteInfoServiceImpl implements ProcessRouteInfoService {

    @Resource
    private ProcessRouteInfoMapper processRouteInfoMapper;

    @Override
    public Long createProcessRouteInfo(ProcessRouteInfoCreateReqVO createReqVO) {
        // 插入
        ProcessRouteInfoDO processRouteInfo = ProcessRouteInfoConvert.INSTANCE.convert(createReqVO);
        processRouteInfoMapper.insert(processRouteInfo);
        // 返回
        return processRouteInfo.getId();
    }

    @Override
    public void batchCreateProcessRouteInfo(List<ProcessRouteInfoCreateReqVO> createReqVOS) {
        //TODO 判断工艺路线是否被其他的产品使用，若被使用，不能变更

        // 工艺路线详细配置中只能有一个关键工序
        List<ProcessRouteInfoCreateReqVO> list = createReqVOS.stream().filter(processRouteInfoCreateReqVO -> processRouteInfoCreateReqVO.getMainProcess().equals(SystemBooleanEnum.YES.getSystemBoolean())).collect(Collectors.toList());
        if (list.size() > 1) {
            throw exception(PROCESS_ROUTE_INFO_MAIN_EXISTS);
        }
        // 工艺路线详细配置中有且只能有一个关键工序
        int count = 0;
        for (int i = 0; i < createReqVOS.size(); i++) {
            ProcessRouteInfoCreateReqVO item = createReqVOS.get(i);
            if (SystemBooleanEnum.YES.getSystemBoolean().equals(item.getMainProcess())) {
                count = count + 1;
            }
            item.setSubsequence(i+1);
        }
        if (count > 1) {
            throw exception(PROCESS_ROUTE_INFO_MAIN_EXISTS);
        }
        ProcessRouteInfoCreateReqVO createReqVO = createReqVOS.get(0);
        // 删除工艺路线配置
        deleteProcessRouteInfoByRouteId(createReqVO.getRouteId());
        // 插入工艺路线配置
        List<ProcessRouteInfoDO> processRouteInfoDOS = ProcessRouteInfoConvert.INSTANCE.convertList03(createReqVOS);
        processRouteInfoMapper.insertBatch(processRouteInfoDOS);
    }

    @Override
    public void updateProcessRouteInfo(ProcessRouteInfoUpdateReqVO updateReqVO) {
        // 校验存在
        validateProcessRouteInfoExists(updateReqVO.getId());
        // 更新
        ProcessRouteInfoDO updateObj = ProcessRouteInfoConvert.INSTANCE.convert(updateReqVO);
        processRouteInfoMapper.updateById(updateObj);
    }

    @Override
    public void deleteProcessRouteInfo(Long id) {
        // 校验存在
        validateProcessRouteInfoExists(id);
        // 删除
        processRouteInfoMapper.deleteById(id);
    }

    @Override
    public void deleteProcessRouteInfoByRouteId(Long routeId) {
        processRouteInfoMapper.deleteByRouteId(routeId);
    }

    private void validateProcessRouteInfoExists(Long id) {
        if (processRouteInfoMapper.selectById(id) == null) {
            throw exception(PROCESS_ROUTE_INFO_NOT_EXISTS);
        }
    }

    @Override
    public ProcessRouteInfoDO getProcessRouteInfo(Long id) {
        return processRouteInfoMapper.selectById(id);
    }

    @Override
    public List<ProcessRouteInfoDO> getProcessRouteInfoList(Collection<Long> ids) {
        return processRouteInfoMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ProcessRouteInfoDO> getProcessRouteInfoPage(ProcessRouteInfoPageReqVO pageReqVO) {
        return processRouteInfoMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ProcessRouteInfoDO> getProcessRouteInfoList(ProcessRouteInfoExportReqVO exportReqVO) {
        return processRouteInfoMapper.selectList(exportReqVO);
    }

    @Override
    public List<ProcessRouteInfoDO> getProcessRouteListByRouteId(Long id) {
        return processRouteInfoMapper.selectList(id);
    }

    @Override
    public List<ProcessRouteInfoRespVO> getProcessRouteInfoListByRouteId(Collection<Long> routeIds) {
        List<ProcessRouteInfoDO> list = processRouteInfoMapper.selectListByRouteId(routeIds);
        return ProcessRouteInfoConvert.INSTANCE.convertList(list);
    }

    @Override
    public boolean checkUseProcess(Long processId) {
        ProcessRouteInfoDO processRouteInfoDO = processRouteInfoMapper.selectOneByProcessId(processId);
        return ObjectUtil.isNotEmpty(processRouteInfoDO);
    }

}
